#include "homepage.h"
#include "ui_homepage.h"
#include "globalval.h"

HomePage::HomePage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HomePage)
{
    ui->setupUi(this);
    this->setWindowTitle("智能车间管控");
    QIcon c(":/new/prefix1/c.png");
    this->setWindowIcon(c);

    //mqtt相关
    MqttHandler *mqtt = new MqttHandler;
    GlobalVal::mqtt = mqtt;
    mqtt->setHost(QHostAddress("192.168.50.133"));
    mqtt->setPort(1883);
    mqtt->connectToHost();

    //
    QPainter painter(this);
    QPixmap map(":/new/prefix1/00.png");
    // 绘制图片
    // 参数1、2 横轴及纵轴坐标
    // 参数3、4 绘制的宽高
    // 参数5    绘制的内容
    painter.drawPixmap(0,0,width(),height(),map);




    //实例化四个界面
    m0Wig = new M0Wig;
    modbusWig = new ModbusWig;
    mbappWig = new MbappWig;
    setWig = new SetWig;
    history = new History;

    //将四个界面依次添加到堆栈窗体
    ui->stackedWidget->addWidget(m0Wig);
    ui->stackedWidget->addWidget(modbusWig);
    ui->stackedWidget->addWidget(mbappWig);
    ui->stackedWidget->addWidget(setWig);
    ui->stackedWidget->addWidget(history);



    //连接列表界面和堆栈窗体的信号
    QObject::connect(ui->listWidget, SIGNAL(currentRowChanged(int)),
                     ui->stackedWidget, SLOT(setCurrentIndex(int)));

    //哪个界面需要接收Mqtt的消息，就订阅这个信号，然后在自己的界面文件中处理消息
    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
                     modbusWig, SLOT(stmSlot(QByteArray)));

    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
                     mbappWig, SLOT(modbusUpSlot(QByteArray)));

    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
                     history, SLOT(hisSlot(QByteArray)));

    connect(m0Wig,SIGNAL(con(QByteArray)),
            this,SLOT(sendSlot(QByteArray)));
    connect(mbappWig,SIGNAL(ctldown32(QByteArray)),
            this,SLOT(sendSlot(QByteArray)));
    connect(modbusWig,SIGNAL(stm(QByteArray)),
            this,SLOT(sendSlot(QByteArray)));
    connect(history,SIGNAL(history(QByteArray)),
            this,SLOT(sendSlot(QByteArray)));
}

HomePage::~HomePage()
{

    delete ui;
}


void HomePage::sendSlot(QByteArray msg)
{
    //    QString mqttser=ui->lineEdit->text();
    //    quint16 serid = mqttser.toUInt();
    qDebug()<< msg;
    QMQTT::Message message(666,"down",msg);
    qDebug()<<"11111---------";

    GlobalVal::mqtt->publish(message);
}
